from system.models import SysUser, SysRole, SysRoleMenu, SysConfig
from django.db.models import Q
import django_filters   

##########  ROLE ############
class RoleFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(
        method='filter_by_name_or_code',
        required=False)
    
    id = django_filters.NumberFilter(
        field_name='id',
        lookup_expr='exact',  # 精确匹配
        required=False  # 非必填
    )

    def filter_by_name_or_code(self, queryset, name, value):
        return queryset.filter(Q(name__icontains=value) | Q(code__icontains=value))
    
    class Meta:
        model = SysRole
        fields = []  # 清空默认字段，改用上方自定义字段

##########  ROLE_MENU ############
class RoleMenuFilter(django_filters.FilterSet):
    role_id = django_filters.NumberFilter(
        field_name='role',  # 使用外键字段名
        lookup_expr='exact',
        required=True
    )
    
    class Meta:
        model = SysRoleMenu
        fields = []  # 清空默认字段，改用上方自定义字段

##########  USER ############
class UserFilter(django_filters.FilterSet):
    username = django_filters.CharFilter(
        field_name='username',
        lookup_expr='icontains',  # 模糊匹配
        required=False  # 非必填
    )
    id = django_filters.NumberFilter(
        field_name='id',
        lookup_expr='exact',  # 精确匹配
        required=False  # 非必填
    )
    status = django_filters.NumberFilter(
        field_name='status',
        lookup_expr='exact',  # 精确匹配
        required=False  # 非必填
    )
    phonenumber = django_filters.CharFilter(
        field_name='phonenumber',
        lookup_expr='icontains',  # 模糊匹配
        required=False  # 非必填
    )
    is_active = django_filters.BooleanFilter(
        field_name='is_active',
        required=False  # 非必填
    )
    user_from = django_filters.NumberFilter(
        field_name='user_from',
        lookup_expr='exact',  # 精确匹配
        required=False  # 非必填
    )
        
    class Meta:
        model = SysUser
        fields = []  # 清空默认字段，改用上方自定义字段
